// ---------------------------------------------------------------------
//
// -- 通用遮罩层
// -- author £
//
// ---------------------------------------------------------------------

cc.Class({
    extends: cc.Component,

    properties: {
        MASK_FADE_TIME: {
            default: 0.2,
            tooltip: '游戏遮罩时间'
        },

        lb: cc.Label,
        iconLoad: cc.Animation,
        text: {
            set: function (value) {
                this.lb.string = value;
            },
        }
    },

    ctor: function () {
        this._isShow = false;
        this._priority = 0;
    },

    // 加载后劫持触摸事件
    onLoad: function () {
        this._maskInitAlpha = this.node.opacity;
        this.node.active = false;
        this.node.on('touchend', this.stopEvent, this);
        this.node.on('touchstart', this.stopEvent, this);
    },

    start: function () {
        
    },

    stopEvent: function (event) {
        console.log('stopEvent');
        event.stopPropagation();
    },

    show: function (opacity, directly, priority) {
        // this.node.off('touchend', this.stopEvent, this);
        // this.node.off('touchstart', this.stopEvent, this);

        // this.node.on('touchend', this.stopEvent, this);
        // this.node.on('touchstart', this.stopEvent, this);

        if (priority === null || priority === undefined)
            priority = 0;

        if (this._priority < priority)
            this._priority = priority;

        if (this._isShow) return;

        if (this.iconLoad) {
            this.iconLoad.play();
        }

        this._isShow = true;
        this.node.active = true;

        if (directly) {
            this.node.opacity = (opacity === undefined || opacity === null) ? 
            this._maskInitAlpha : opacity;
            return;
        }

        this.node.opacity = 0;
        this.node.runAction(cc.fadeTo(this.MASK_FADE_TIME, 
            (opacity === undefined || opacity === null) ? this._maskInitAlpha : opacity));
    },

    hide: function (priority) {
        if (priority === null || priority === undefined)
            priority = 0;

        if (priority < this._priority)
            return;
        
        if (this.iconLoad) {
            this.iconLoad.stop();
        }

        this._priority = 0;
        this._isShow = false;
        this.node.active = false;
    }

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
